package org.ansel.webnodb.demo.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ansel.webnodb.demo.service.DemoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

@Controller
@RequestMapping("/demo")
public class DemoController {
	private static final Logger LOGGER = LoggerFactory.getLogger(DemoController.class);
	@Autowired
	private DemoService demoService;
	@RequestMapping("/testProp")
	public String testProp() {
		demoService.testProp();
		return "myip";
	}
	@ResponseBody
	@GetMapping("/getProp")
	public String getProp() {
		Map<String,String> envmap = System.getenv();
		JSONObject j = (JSONObject) JSONObject.toJSON(envmap);
		j.put("user.dir",System.getProperty("user.dir"));
		return j.toString();
	}
	@ResponseBody
	@GetMapping("/testChinese")
	public String testChinese() {
		String cnstr="#🇺🇸_US_美国->🇫🇷_FR_法国";
		String ret=("orig_"+cnstr.contains("美国"));
		try {
			cnstr=new String(cnstr.getBytes(),"UTF-8");
			ret+=("UTF-8_"+cnstr.contains("美国"));
			cnstr=new String(cnstr.getBytes(),"GB2312");
			ret+=("GB2312_"+cnstr.contains("美国"));
			cnstr=new String(cnstr.getBytes(),"GBK");
			ret+=("GBK_"+cnstr.contains("美国"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		LOGGER.info(ret);
		return ret;
	}
	/** 通用的页面跳转
	 * @param req
	 * @param pagename
	 * @return
	 */
	@RequestMapping("/{pagename}")
	public String pagename(HttpServletRequest req,@PathVariable String pagename) {
		JSONObject j = (JSONObject) JSONObject.toJSON(req.getParameterMap());
		System.out.println(j.toString());
		return pagename;
	}
	@PostMapping("/uploadfile")
	public View uploadfile(@RequestParam("dir") MultipartFile[] dir,@RequestParam("file") MultipartFile[] file) {
		// ,
		for (MultipartFile f : dir) {
			String filename = f.getOriginalFilename();
			if("".equals(filename))
				continue;
			File file1 = null;
			file1 = new File(System.getProperty("user.dir") + "/tmpfiles/" + filename);
			file1.mkdirs();
			try {
				file1.createNewFile();
				f.transferTo(file1);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		for (MultipartFile f : file) {
			String filename = f.getOriginalFilename();
			if("".equals(filename))
				continue;
			File file1 = null;
			file1 = new File(System.getProperty("user.dir") + "/tmpfiles/" + filename);
			file1.mkdirs();
			try {
				file1.createNewFile();
				f.transferTo(file1);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return new RedirectView("filemgr");//"redirect:filemgr"
	}
}
